<?php

/*

  Action - Attack	$5

  +3 Cards
  Each other player chooses one: he discards 2 cards;
  or he gains a Curse card, putting it in his hand.

 */

class Torturer extends Card implements iAction, iKingdom, iAttack, iIntrigue {

    public function __construct() {
        parent::__construct("Torturer", 5);
        $this->undoPlay = false;
        $this->undoBuy = true;
    }

    public function getDescription() {
        return array(
            "+3 Cards",
            "Each other player chooses one: he discards 2 cards,  or he gains a Curse card, putting it in his hand.");
    }
    
    public function play(Game $game, $isRealCard) {

        $drawed_cards = $game->drawCards(3);
        ScreenDisplay::addScreenMessage($game, true, "draw  " . nicePrint($drawed_cards), "draws ".sizeof($drawed_cards) ." cards");
        $game->setPhase(Phase_Sleep);
       
        $game->attack($this);

        $names = $game->getOtherPlayersNames(true);

        foreach ($names as $name) {

            $options = array(new Option(1, "Discard 2 cards"), new Option(2, "Gain Curse in hand"));
            $output = MySocket::GetUserReply($game, Input_ChooseOne, $options, 1, 1, null, "Please choose", $name, true);
        }

        //the waiting function
        $users_replies = MySocket::getUsersReplies($game);
        foreach ($users_replies as $name => $output) {

            $answer = $output[0];
            if ($answer == "Discard 2 cards") {

                $playHandOther = $game->getPlayHand($name);
                $playHandSize = sizeof($playHandOther);
                $cards_to_discard = min(2, $playHandSize);                
                if ($cards_to_discard > 0) {
                    
                    $output = MySocket::GetUserReply($game, Input_ChooseMany, $playHandOther, $cards_to_discard, $cards_to_discard, null, "Discard $cards_to_discard cards", $name, true);
                } else {
                    $game->setPhase(Phase_Sleep, $name);
                }
            } else if ($answer == "Gain Curse in hand") {
                $curse = $game->moveCardFromPileToPile("Curse", BoardCards, PlayHand, $name);
                 ScreenDisplay::addScreenMessage($game, true, "gain ".  nicePrint($curse)." in hand", "gains ".ScreenDisplay::$LMSG." in hand", $name);
                
                $game->setPhase(Phase_Sleep, $name);
            }
        }

        //the waiting function
        $users_replies = MySocket::getUsersReplies($game);
        foreach ($users_replies as $name => $output) {
             ScreenDisplay::addScreenMessage($game, true, "discard ".  nicePrint($output), "discards ".sizeof($output)." cards", $name);
            foreach ($output as $card_name) {
                $game->moveCardFromPileToPile($card_name, PlayHand, DiscardPile, $name);
            }

            $game->setPhase(Phase_Sleep, $name);
        }

        $game->cardActionFinished();
        return true;
    }

}

?>